/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.fpt.xml.hrm.common;

import java.io.File;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import vn.edu.fpt.xml.hrm.model.EmployeeBean;
import vn.edu.fpt.xml.hrm.model.ProjectBean;
import vn.edu.fpt.xml.hrm.pojo.Employees;
import vn.edu.fpt.xml.hrm.pojo.Projects;

/**
 *
 * @author HoangTN
 */
public class Marshall {

    public void getAllProjectToXML(String filePath) {
        try {
            ProjectBean bean = new ProjectBean();
            Projects projects = new Projects();
            projects.setProject(bean.getAllProject());
            marshall(filePath, projects);
        } catch (SQLException ex) {
            Logger.getLogger(Marshall.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getAllEmployeeToXML(String filePath) {
        try {
            EmployeeBean bean = new EmployeeBean();
            Employees emps = new Employees();
            emps.setEmployee(bean.getAllEmployee());
            marshall(filePath, emps);
        } catch (SQLException ex) {
            Logger.getLogger(Marshall.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void marshall(String filePath, Object obj) {
        try {
            JAXBContext ctx = JAXBContext.newInstance(obj.getClass());
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(obj, new File(filePath));
        } catch (JAXBException ex) {
            ex.printStackTrace();
        }
    }
}
